// pages/my/order-detail/index.js
import {
  _headerPost,
  config
} from '../../../utils/httpUtils.js'
// const http = require('../../../utils/httpUtils.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ip: config.host,
    modal: true,
    value: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getDetail(options.orderId)
  },
  //获取订单详情
  getDetail(id) {
    _headerPost('/index/Order/detail', {
      id: id
    }, res => {
      if (res.data.code == 1) {
        this.setData({
          orderInfo: res.data.data,
          orderState: this.isState(res.data.data)
        })
      }
    })
  },
  //判断状态
  isState(body) {
    if (body.o_type == 'goods')
      switch (body.o_state) {
        case 0:
          return 0;
          break
        case 1:
          if (body.oe_id == 0)
            return 1
          else return 2
          break
        case 2:
          break
        case 3:
          break
        case 4:
          if (body.orec_id > 0 && body.oref_state > 0) { // 查看详情
            return 6
          } else if (body.orec_id == 0 && body.oref_state > 0) { // 评价
            return 3
          } else if (body.orec_id > 0 && body.oref_state == 0) { // 售后
            return 4
          } else if (body.orec_id == 0 && body.oref_state == 0) { // 可售后，可评价
            return 5
          }
          break
        case 5:
          return 6
          break
      } else {
        return 7
      }
  },
  //立即支付
  buyNow(order) {
    let pay = {}
    if (this.data.orderInfo.opa_id == null) {
      let data = {}
      data.oId = this.data.orderInfo.o_id
      data.type = 'order'
      _headerPost('/index/Order/initorder', data, res => {
        if (res.data.code == 1)
          wx.navigateTo({
            url: '../../public/topay/index?payInfo=' + JSON.stringify(res.data.data)
          })
      })
    } else {
      pay.pay_money = this.data.orderInfo.o_pay_money
      pay.opa_id = this.data.orderInfo.opa_id
      wx.navigateTo({
        url: '../../public/topay/index?payInfo=' + JSON.stringify(pay)
      })
    }
  },
  //查看核销码
  openCode() {
    -_headerPost('index/Order/viewVerifyCode', '', res => {
      if (res.data.code == 1)
        wx.showModal({
          title: '核销码',
          content: res.data.data.o_verify_code,
          showCancel: false,
          confirmColor: '#E9383C'
        })
      else wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
    })
  },
  //申请售后或者查看售后
  afterSale(e) {
    let data = this.data.orderInfo.o_goods[e.currentTarget.id]
    data.oId = this.data.orderInfo.o_id
    data.o_order = this.data.orderInfo.o_order
    if (this.data.orderState == 3) {
      wx.navigateTo({
        url: '../sale-detail/index?data=' + JSON.stringify(data)
      })
    } else
      wx.navigateTo({
        url: '../after-sale/index?data=' + JSON.stringify(data)
      })
  },
  //删除订单
  delOrder() {
    _headerPost('/index/Order/delete', {
      id: this.data.orderInfo.o_id
    }, res => {
      if (res.data.code == 1) {
        wx.showToast({
          title: res.data.msg,
        })
        wx.navigateBack({})
      } else {
        wx.showToast({
          title: res.data.msg,
        })
      }
    })
  },
  //追加评价
  Evaluation(e) {
    let index = e.currentTarget.id
    let data = this.data.orderInfo.o_goods[index]
    data.shopId = this.data.orderInfo.shop_id
    data.oId = this.data.orderInfo.o_id
    wx.navigateTo({
      url: '../evaluate/index?data=' + JSON.stringify(data),
    })
  },
  //评价
  Evaluate() {
    let data = this.data.orderInfo.o_goods[0]
    data.shopId = this.data.orderInfo.shop_id
    data.oId = this.data.orderInfo.o_id
    wx.navigateTo({
      url: '../evaluate/index?data=' + JSON.stringify(data),
    })
  },
  //查看物流
  lookLogistics() {
    http._headerPost('index/Order/logistics', {
      id: this.data.orderInfo.o_id
    }, res => {
      // "url": "http://m.kuaidi100.com/result.jsp?com=huitong&nu=71145909509187", //请求快递100接口
      // "flag": "huitong", //快递标识
      // "num": "71145909509187", //快递单号
      // "name": "百世汇通快递" //快递名称
    })
  },
  //提醒发货
  remindDeliver() {
    wx.showToast({
      title: '提醒成功',
    })
  },
  confirmGoods(id) {
    //确认收货
    _headerPost('/index/Order/affirm', {
      id: this.data.orderInfo.o_id
    }, res => {
      if (res.data.code == 1) {
        wx.showModal({
          title: '提示',
          content: '收货成功',
          showCancel: false
        })
        wx.navigateBack({})
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  //评价分数
  sliderChange(e) {
    console.log(e.detail.value)
    this.setData({
      slider: e.detail.value,
    })
  }
})